<template lang="pug">
data-page(
  v-bind="pageParams"
)
  h3(style="text-align:center") 危急值消息查询统计表
  mt-table(
    v-bind="tableProps"
  )
</template>

<script setup lang="ts">
import {
  type FormItemProps,
  type IAction,
  type DataPageProps,
  type TableProps,
  useNotify,
  type Action,
  Icon,
} from "@scmt/common-components";
import { useQuery } from "@scmt/common-hooks";
import {
  type ApiBasedOptions,
  type QueryPagination,
  useApi,
} from "@scmt/core-api";
import {
  type SpecimenEntity,
  type SpecimenQueryParams,
} from "@scmt/api-lis-specimen";

const $api = useApi();
const $notify = useNotify();
const $utils = useUtils();
const queryPramas = reactive<SpecimenQueryParams>({
  state: 0,
  startDate: $utils.date().startOf("date").format("YYYY-MM-DD HH:mm:ss"),
  endDate: $utils.date().endOf("date").format("YYYY-MM-DD HH:mm:ss"),
  pageNum: 1,
  pageSize: 20,
});
const editingObj = ref<null | SpecimenEntity>(null);

const selectEquipment = defineModel();
const selectionRow = ref([]);
import { useUtils } from "@scmt/lib-utils";

const { query, data, loadingMap } = useQuery({
  tableData: (q: any) => {
    const params = Object.assign({}, queryPramas, q);
    return $api.lis.specimen.query(params);
  },
  save: (model: SpecimenEntity) => {
    return $api.lis.specimen.save(model).then(() => {
      $notify.success("保存成功");
      editingObj.value = null;

      query("tableData", {
        pageSize: 1,
      });
    });
  },
  destroy: (ids: string) => {
    return $api.lis.specimen.destroy(ids).then(() => {
      $notify.success("删除成功");

      query("tableData", {
        pageSize: 1,
      });
    });
  },
});

onMounted(() => {
  query("tableData");
});

const pageParams = computed<DataPageProps>(() => {
  const queryFields: FormItemProps[] = [
    {
      prop: "orgId",
      label: "机构",
      as: "select",
      options: {
        loadMethod: () => $api.upms.org.listAllOptions(),
      },
    },
    {
      prop: "inspectionTime",
      label: "审核时间",
      as: "date",
      options: {
        type: "datetimerange",
      },
    },
    {
      prop: "orgId",
      label: "小组仪器",
      as: "select",
      options: {
        loadMethod: () => $api.lis.instrument.listAsOptions(),
      },
    },
  ];

  const dataPageProps: DataPageProps = {
    loading: loadingMap.tableData,
    queryModel: queryPramas,
    queryFields,
    onQuery: () => query("tableData"),
    onAct: onPageActions,
    flex: true,
  };

  return dataPageProps;
});

const tableData = [
  {
    collectionTime: "",
    instrument: "",
    sampleNumber: "",
    patientName: "",
    outpatientNumber: "",
    barcode: "",
    department: "",
    bedNumber: "",
    reportProject: "",
    result: "",
    reference: "",
    reportTime: "",
    inspector: "",
    reviewer: "",
    reportOpinions: "",
    confirmingPerson: "",
    reportDoctorTime: "",
    processingPerson: "",
    processingTime: "",
    handlingSuggestions: "",
  },
];

const tableProps = computed(() => {
  const tableProps: TableProps = {
    loading: loadingMap.tableData,
    data: tableData,
    insertIndex: false,
    columns: [
      {
        prop: "collectionTime",
        label: "核收日期",
      },
      {
        prop: "instrument",
        label: "仪器",
      },
      {
        prop: "sampleNumber",
        label: "样本号",
      },
      {
        prop: "patientName",
        label: "姓名",
      },
      {
        prop: "outpatientNumber",
        label: "住院号/门诊号",
      },
      {
        prop: "barcode",
        label: "条码号",
      },
      {
        prop: "department",
        label: "科室",
      },
      {
        prop: "bedNumber",
        label: "床号",
      },
      {
        prop: "reportProject",
        label: "报告项目",
      },
      {
        prop: "result",
        label: "检验结果",
      },
      {
        prop: "reference",
        label: "参考值",
      },
      {
        prop: "reportTime",
        label: "报告时间",
      },
      {
        prop: "inspector",
        label: "检验人",
      },
      {
        prop: "reviewer",
        label: "审核人",
      },
      {
        prop: "reportOpinions",
        label: "报告意见",
      },
      {
        prop: "confirmingPerson",
        label: "确认人",
      },
      {
        prop: "reportDoctorTime",
        label: "护士报告医生时间",
      },
      {
        prop: "processingPerson",
        label: "处理人",
      },
      {
        prop: "processingTime",
        label: "处理时间",
      },
      {
        prop: "handlingSuggestions",
        label: "处理意见",
      },
    ],
    onAct: onPageActions,
    onPaginate: (pagination: QueryPagination) => query("tableData", pagination),
    onRowClick: (row: any) => {
      selectEquipment.value = row;
    },
    onSelectionChange: (selection: any) => {
      selectionRow.value = selection;
    },
    highlightCurrentRow: true,
  };
  return tableProps;
});

const onPageActions = (action: IAction, model?: any) => {};
</script>

<style lang="less" scoped></style>
